<?php

require_once('functions/pageLoad.php');
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];

// tradesman info

$additionalMeta = '<link rel="stylesheet" type="text/css" href="/library/slimbox.css" media="screen" />
<script type="text/javascript" src="/library/mootools.js"></script>
<script type="text/javascript" src="/library/slimbox.js"></script>';

//Change By Vyas Ishan 17 May 2013 Task2 
#Added the meta_keyword and meta_description field in the query
$sql = "SELECT tradesman.meta_keyword, tradesman.meta_description,user_id, tradesman.address_id, company_name, username, description, telephone, mobile, website, trading_name, company_type, reg, vat, established, string, logo_id 
	FROM tradesman 
	LEFT JOIN images ON tradesman.logo_id = images.id 
	LEFT JOIN users ON tradesman.user_id = users.id 
	WHERE tradesman.id = '".mysql_real_escape_string($_GET['id'])."' LIMIT 0,1";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$tradesman_rs=mysql_fetch_assoc($query);

//Change By Vyas Ishan 17 May 2013 Task2 
//Set the keyword and description for seo friendly and fetch from tradesmen table
$META_KEYWORDS = $tradesman_rs['meta_keyword'];
$page_description = $tradesman_rs['meta_description'];


if(!isset($_GET['trades'])){


header("Location: ".build_tradesman_link($tradesman_rs['company_name'], $_GET['id']), true, 301);

}



$sql = "SELECT area_id, area, line_1, line_2, line_3, postcode, addresses.longitude, addresses.latitude
	FROM addresses 
	LEFT JOIN areas ON addresses.area_id = areas.id 
	WHERE addresses.id = '".$tradesman_rs['address_id']."' LIMIT 0,1";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$address_rs=mysql_fetch_assoc($query);

if($address_rs['latitude'] == 0){

$sql = "SELECT longitude, latitude
	FROM areas 
	WHERE id = '".$address_rs['area_id']."' LIMIT 0,1";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$coords_rs=mysql_fetch_assoc($query);

$address_rs['latitude'] = $coords_rs['latitude'];
$address_rs['longitude'] = $coords_rs['longitude'];

}

$additionalMeta.='
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='.$settings['google_api_key'].'&amp;sensor=false" type="text/javascript"></script>
<script type="text/javascript">
function initialize(){
	if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("tradesman_map"));
        map.setCenter(new GLatLng('.$address_rs['latitude'].', '.$address_rs['longitude'].'), 9);
        var marker = new GMarker(new GLatLng('.$address_rs['latitude'].', '.$address_rs['longitude'].'));
        map.addOverlay(marker);
	}
}
</script>';

// images

$sql = "SELECT string, caption FROM images WHERE tradesman_id = '".mysql_real_escape_string($_GET['id'])."' AND id != '".$tradesman_rs['logo_id']."' LIMIT 0,5";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$img_rows = mysql_num_rows($query);

if($img_rows > 0){
$tradesman_images.='<div id="tradesman_images">';
}

if($img_rows > 1){
$pp_gal = '[pp_gal]';
}

$image_i = 1;

while($image_rs=mysql_fetch_assoc($query)){
$image_class = ($image_i < 5) ? ' class="tradesman_image"' : '' ;
$tradesman_images.='<a href="/public_images/large/'.$image_rs['string'].'" rel="prettyPhoto'.$pp_gal.'" title="'.$image_rs['caption'].'"'.$image_class.'><img src="/public_images/small/'.$image_rs['string'].'" alt="" width="195" /></a>';
$image_i++;
}

if(mysql_num_rows($query) > 0){
$tradesman_images.='</div><div class="divider"><hr /></div>';
}

// trades

$sql = "SELECT trade 
	FROM tradesman_to_trade 
	LEFT JOIN trades ON tradesman_to_trade.trade_id = trades.id 
	WHERE tradesman_id = '".mysql_real_escape_string($_GET['id'])."' ORDER BY trade";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$trade_rows = mysql_num_rows($query);

$i = 1;

while($trade_rs=mysql_fetch_assoc($query)){

$trades.=$trade_rs['trade'];

$trades.=($i != $trade_rows)? ', ' : '' ;

$i++;

}

// coverage
//Change By Vyas Ishan 24 May 2013 Task2 added the status field condition
$sql = "SELECT radius FROM tradesman
		LEFT JOIN subscriptions ON tradesman.subscription_id = subscriptions.id
		WHERE   subscriptions.status = '1'  AND tradesman.id = '".mysql_real_escape_string($_GET['id'])."'";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$area_rs=mysql_fetch_assoc($query);
$coverage = $area_rs['radius'].' miles radius';

// admin feedback

$feedback_content = '';

$sql = "SELECT job_id, title, rating, review, date_reviewed, trade, area FROM reviews 
	LEFT JOIN jobs ON reviews.job_id = jobs.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE tradesman_id = '".mysql_real_escape_string($_GET['id'])."' AND job_id = '0' AND reviews.user_id = '0' ORDER BY date_reviewed";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$feedback_rows1 = mysql_num_rows($query);

$i = 1;

while($feedback_rs=mysql_fetch_assoc($query)){

$first_ones_style = ($i == 1) ? ' style="padding-top: 0px;"' : '';

$feedback_content.='<div class="feedback_div"'.$first_ones_style.'>
<p>'.ucfirst($feedback_rs['review']).'</p>
<p style="margin: 0px; padding: 0px;"><img src="/images/'.$feedback_rs['rating'].'-star.gif" alt="'.($feedback_rs['rating'] / 2).'/5" /></p>
<p style="margin-top: 0px; padding-top: 0px;"><small>'.date("jS F Y",strtotime($feedback_rs['date_reviewed'])).'</small></p>
</div>';

$i++;

}

// user feedback

$sql = "SELECT job_id, title, rating, review, date_reviewed, trade, area FROM reviews 
	LEFT JOIN jobs ON reviews.job_id = jobs.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	WHERE tradesman_id = '".mysql_real_escape_string($_GET['id'])."' AND job_id != '0' AND reviews.user_id != '0' ORDER BY date_reviewed DESC";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$feedback_rows2 = mysql_num_rows($query);

$i = 1;

while($feedback_rs=mysql_fetch_assoc($query)){

$job_link = (strlen($feedback_rs['title']) > 0) ? '<a href="'.build_job_link($feedback_rs['trade'],$feedback_rs['area'],$feedback_rs['job_id']).'">'.ucfirst($feedback_rs['title']).'</a>' : '' ;

$last_ones_style = ($i == $feedback_rows2) ? ' style="border-bottom: none;"' : '' ;

$feedback_content.='<div class="feedback_div"'.$last_ones_style.'>
'.$job_link.'
<p>'.ucfirst($feedback_rs['review']).'</p>
<p style="margin: 0px; padding: 0px;"><img src="/images/'.$feedback_rs['rating'].'-star.gif" alt="'.($feedback_rs['rating'] / 2).'/5" /></p>
<p style="margin-top: 0px; padding-top: 0px;"><small>'.date("jS F Y",strtotime($feedback_rs['date_reviewed'])).'</small></p>
</div>';

$i++;

}

$feedback_rows = $feedback_rows1 + $feedback_rows2;

$feedback_plural = ($feedback_rows != 1) ? 's' : '' ;

$feedback_content = (strlen($feedback_content) > 0) ? $feedback_content : '<p>'.ucfirst(htmlentities($tradesman_rs['company_name'])).' has not had any feedback.</p>';

$bodyTag = ' onload="initialize()" onunload="GUnload()"';

$average_feedback = tradesman_average_feedback($_GET['id']);

$content = '<div id="tradesman_info">';

$content.= (strlen($tradesman_rs['string']) > 0) ? '<img src="/public_images/logos/'.$tradesman_rs['string'].'" alt="'.ucfirst(htmlentities($tradesman_rs['company_name'])).'\'s logo" />' : '' ;

$content.= '<div class="tradesman_info_wrap" style="margin-top: 10px; display: inline;">
<div class="tradesman_info_left">Company:</div>
<div class="tradesman_info_right">'.ucfirst(htmlentities($tradesman_rs['company_name'])).'. '.$trades.' in '.$address_rs['area'].' ('.$tradesman_rs['company_type'].')</div>
</div>

<div class="tradesman_info_wrap">
<div class="tradesman_info_left">Established:</div>
<div class="tradesman_info_right">'.date("jS F Y",strtotime($tradesman_rs['established'])).'</div>
</div>

<div class="tradesman_info_wrap">
<div class="tradesman_info_left"><span style="position: relative; top: 4px;">Average rating:</span></div>
<div class="tradesman_info_right"><img src="/images/'.$average_feedback.'-star.gif" alt="'.($average_feedback / 2).'/5" /> <span style="position: relative; top: -3px; left: 4px;">('.$feedback_rows.' rating'.$feedback_plural.')</span></div>
</div>';

$content.= '
<div class="tradesman_info_wrap">
<div class="tradesman_info_left">Trades:</div>
<div class="tradesman_info_right">'.$trades.'</div>
</div>

<div class="tradesman_info_wrap">
<div class="tradesman_info_left">Coverage:</div>
<div class="tradesman_info_right">'.$coverage.'</div>
</div>';

/*

// quoted user?

$sql = "SELECT quotes.id FROM quotes 
	LEFT JOIN jobs ON quotes.job_id = jobs.id 
	LEFT JOIN reviews ON jobs.id = reviews.job_id 
	WHERE jobs.user_id = '".$user_id."' AND quotes.tradesman_id = '".mysql_real_escape_string($_GET['id'])."' AND quotes.status = 'Accepted' AND reviews.id IS NULL";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$quoted_user_rows = mysql_num_rows($query);

if($quoted_user_rows > 0 || $tradesman_rs['user_id'] == $user_id){

*/

$address.= (strlen($address_rs['line_1']) > 0) ? $address_rs['line_1'].'<br />' : '' ;
$address.= (strlen($address_rs['line_2']) > 0) ? $address_rs['line_2'].'<br />' : '' ;
$address.= (strlen($address_rs['line_3']) > 0) ? $address_rs['line_3'].'<br />' : '' ;
$address.= (strlen($address_rs['area']) > 0) ? $address_rs['area'].'<br />' : '' ;
$address.= (strlen($address_rs['postcode']) > 0) ? strtoupper($address_rs['postcode']) : '' ;

$telephone = (strlen($tradesman_rs['telephone']) == 0 && strlen($tradesman_rs['mobile']) > 0) ? $tradesman_rs['mobile'] : $tradesman_rs['telephone'] ;

if(strlen($telephone) > 0){
	$content.= '<div class="tradesman_info_wrap">
	<div class="tradesman_info_left">Telephone:</div>
	<div class="tradesman_info_right">'.$telephone.'</div>
	</div>';
}

$content.= '<div class="tradesman_info_wrap">
<div class="tradesman_info_left">Address:</div>
<div class="tradesman_info_right">'.$address.'</div>
</div>';

if(strlen($tradesman_rs['website']) > 0){

$content.= '<div class="tradesman_info_wrap">
<div class="tradesman_info_left">Website:</div>
<div class="tradesman_info_right"><a href="http://'.$tradesman_rs['website'].'" target="_blank">'.$tradesman_rs['website'].'</a></div>
</div>';

}

/*

}

else{

$content.= '<div class="tradesman_info_wrap">
<div class="tradesman_info_left">Contact info:</div>
<div class="tradesman_info_right">Available to quoted users.</div>
</div>';

}

*/

$content.= '<a href="/request_quote.php?tid='.$_GET['id'].'" style="float: left; margin-top: 10px;"><img src="/images/request_a_quote.gif" alt="Request a quote" /></a>

</div>
<div id="tradesman_map"></div>
<div class="divider"><hr /></div>
<h2>Company description</h2>
<div id="tradesman_description" style="padding-bottom: 20px;">'.str_replace("\r\n","<br />",htmlentities(ucfirst($tradesman_rs['description']))).'</div>
<div class="divider"><hr /></div>
'.$tradesman_images.'
<h2>'.ucfirst(htmlentities($tradesman_rs['company_name'])).'. '.$trades.' in '.$address_rs['area'].' - latest feedback</h2>
'.$feedback_content.'

';

$page_title = ucfirst(htmlentities($tradesman_rs['company_name'])).' - '.$trades.' in '.$address_rs['area'];

include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1><?php echo ucfirst(htmlentities($tradesman_rs['company_name'])).', '.$address_rs['area']; ?></h1>
<h2 class="tradesman_header"><?php echo $trades.' in '.$address_rs['area']; ?></h2>

<?php echo $content; ?>

<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>